//
//  JXOrderDetailController.m
//  JiaoX
//
//  Created by yinqy on 15/1/19.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXOrderDetailController.h"
#import "ShareOrderPopView.h"
#import "SelfDataTool.h"
#import "CustomShowView.h"
#import "OrderProdItemView.h"
#import "OrderDetailProdItemCell.h"
#import "OrderDetailHeaderView.h"
#import "JXNewTopicCtl.h"
#import "OrderDetailFooterView.h"
#import "OrderDetailBottomView.h"
#import "JXProdDetailCtl.h"
#import "CartDataTool.h"
#import "CartOrderStatusController.h"
#import "JXAccountCenterController.h"
#import "JXOrderDeliveryController.h"

@interface JXOrderDetailController ()<UITableViewDataSource,UITableViewDelegate,OrderDetailBottomViewDelegate,AccountCenterDelegate,CartOrderStatusDelegate,ShareOrderPopViewDelegate>
{
    NSMutableArray *_prodArray;
    OrderDetailHeaderView *_headerView;
    OrderDetailFooterView *_footerView;
    OrderDetailBottomView *_bottomView;
    
    AddressDetail *_detail;
    
}
@property (nonatomic,weak)UITableView *tableView;
@end

@implementation JXOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"订单详情"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UITableView *tableView = [[UITableView alloc]init];
    [tableView setFrame:self.view.frame];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [self requestData];
}

-(void)requestData{
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.navigationController.view];
    [SelfDataTool getOrderDetailAddress:self.orderItem.orderCode success:^(AddressDetail *result) {
        [HUD hide:YES];
        if ([Utils isBlankString:result.data]) {
            //在这添加数据
            [self inflateData:result];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"请求失败"];
    }];
}

-(void)inflateData:(AddressDetail *)detail{
    _detail = detail;
    _bottomView = [[OrderDetailBottomView alloc]init];
    _bottomView.y = JXScreenH-_bottomView.height;
    _bottomView.delegate = self;
    [_bottomView showCancelBtn:self.orderItem.payMode status:self.orderItem.orderStatus :detail.logistics!=nil];
    [self.view addSubview:_bottomView];
    
    _headerView = [[OrderDetailHeaderView alloc]init];
    [_headerView setItem:self.orderItem];
    [_headerView setAddressDetail:detail];
    
    [self.tableView setTableHeaderView:_headerView];
    
    
    _footerView = [[OrderDetailFooterView alloc]init];
    [_footerView setTitle:self.orderItem];
    [self.tableView setTableFooterView:_footerView];
    
    _prodArray = [NSMutableArray arrayWithArray:self.orderItem.prodList];
    [self.tableView reloadData];
    
}

-(void)viewDidLayoutSubviews
{
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsMake(0,JXPublicInset*2+ORDER_ITEM_IMAGE_WIDTH,0,0)];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger result = _prodArray==nil?0:_prodArray.count;
    if(result){
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }else{
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return result;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailProdItemCell *cell = [OrderDetailProdItemCell cellWithTableView:tableView];
    [cell.orderProdItemView setCartProdSub:_prodArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //跳转到商品详情
    CartProdSub *sub = _prodArray[indexPath.row];
    JXProdDetailCtl *ctrl = [[JXProdDetailCtl alloc]initWithPordId:sub.prodId];
    [self.navigationController pushViewController:ctrl animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ORDER_ITEM_VIEW_HEIGHT;
}

-(void)cancelOrder:(NSString *)orderCode{
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.navigationController.view];
    [SelfDataTool cancelOrder:orderCode success:^(JXBaseResponse *result) {
        [HUD hide:YES];
        if ([Utils isBlankString:result.data]) {
            [DialogUtils showTipsWithView:self.navigationController.view String:@"订单取消成功"];
            [_bottomView setHidden:YES];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"订单取消失败"];
    }];
}

-(void)cancelBtnClick:(UIButton *)btn{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定取消订单？" message:@"" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"不取消",nil];
    [alert show]; //显示退出对话框
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self cancelOrder:self.orderItem.orderCode];
        //直接改变订单状态为取消
        _headerView.item.orderStatus = 5;
        [_headerView setItem:_headerView.item];
    }
}

-(void)changePayBtnClick:(UIButton *)btn{
    //如果支付方式为货到付款，状态为待确认
    if(_orderItem.payMode==PAYMODE_CARGO_PAY && _orderItem.orderStatus==1){
        //在这写改为在线支付的逻辑
        [self payWithPayMode:PAYMODE_ALIPAY];
    }else if (_orderItem.payMode!=PAYMODE_CARGO_PAY && _orderItem.orderStatus==1){
        //在这写立即支付的逻辑
        [self payWithPayMode:_orderItem.payMode];
    }
}

-(void)sunSingleBtnClick:(UIButton *)btn{
    //晒单的按钮点击了
    [self getShareOrderData]; 
}

//立即支付
-(void)payWithPayMode:(int)payMode{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:_orderItem.orderCode forKey:@"orderCode"];
    MBProgressHUD *loding = [DialogUtils showLoadingWithView:self.navigationController.view];
    [CartDataTool getAccountCenterDataWithParam:param success:^(AddressAndCheckProdInfo *result) {
        [loding hide:YES];
        if([Utils isBlankString:result.data]){//获取结算中心需要的数据成功
            result.currentPayMode = payMode;
            result.isImmediatelyPay = YES;
            JXAccountCenterController *ctrl = [JXAccountCenterController initWithProdInfo:result :ACCOUNT_CENTER_ORDER_DETAIL];
            ctrl.delegate = self;
            [self.navigationController pushViewController:ctrl animated:YES];
            
        }
    } failure:^(NSError *error) {
        [loding hide:YES];
    }];

}

-(void)orderDeliveryBtnClick:(UIButton *)btn{
    //查看订单物流信息
    JXOrderDeliveryController *ctrl = [[JXOrderDeliveryController alloc]initWithDelivery:_detail.logistics];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)accountCenterFinish:(int)type status:(NSString *)status{
    if (type==ACCOUNT_CENTER_ORDER_DETAIL) {
        CartOrderStatusController *cart = [CartOrderStatusController initWithResultCode:status type:ACCOUNT_CENTER_ORDER_DETAIL];
        cart.delegate = self;
        [self.navigationController pushViewController:cart animated:YES];
    }
}

-(void)orderStatusFinish:(int)type{
    if (type==ACCOUNT_CENTER_ORDER_DETAIL) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


//以下是晒单需要的方法
-(void)getShareOrderData{
    JXBaseRequest *request = [[JXBaseRequest alloc]init];
    NSDictionary *dic = [request keyValues];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    [params setObject:@"1" forKey:@"pageIndex"];
    MBProgressHUD *loding = [DialogUtils showLoadingWithView:self.view];
    [SelfDataTool getShareOrderList:params success:^(ShareProductRes *result){
        [loding hide:YES];
        //初始化要显示的view
        ShareOrderPopView *shareOrderView =  [[ShareOrderPopView alloc]init];
        shareOrderView.width = JXScreenW - 60;
        [shareOrderView setDelegate:self];
        //设置数据
        [shareOrderView setShareProductData:result];
        //显示在弹出窗体中
        CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:shareOrderView];
        [showView showWithanim:NO];
    } failure:^(NSError *error) {
        [loding hide:YES];
        [DialogUtils showTipsWithView:self.view String:@"加载晒单数据失败"];
    }];
}

-(void)shareOrder:(CartProdSub *)prod{
    JXNewTopicCtl *ctl = [[JXNewTopicCtl alloc]initWithSharedId:KEY_SUNSINGLE_ID prodId:prod.prodId ProdName:prod.prodName];
    [self.navigationController pushViewController:ctl animated:YES];
}


@end
